<template>
  <el-dialog
    :title="!dataForm.invitationCode ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">>
      <el-form-item label="状态" prop="invitationStatus">
        <el-select v-model="dataForm.invitationStatus" placeholder="请选择状态" style="width: 100%">
          <el-option label="有效" value="ACTIVE"></el-option>
          <el-option label="无效" value="INACTIVE"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="邀请码" prop="invitationAuthCode">
        <el-input v-model="dataForm.invitationAuthCode" placeholder="邀请码"></el-input>
      </el-form-item>
      <el-form-item label="过期时间" prop="invitationExpireTime">
        <el-date-picker
          v-model="dataForm.invitationExpireTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择过期时间"
          style="width: 100%">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="clearData">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        invitationCode: '',
        invitationStatus: 'ACTIVE',
        invitationAuthCode: '',
        invitationExpireTime: '',
        extInfo: '',
        env: ''
      },
      dataRule: {
        invitationStatus: [
          { required: true, message: '状态不能为空', trigger: 'change' }
        ],
        invitationAuthCode: [
          { required: true, message: '权限码不能为空', trigger: 'blur' }
        ],
        invitationExpireTime: [
          { required: true, message: '过期时间不能为空', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/invitation/invitation/queryWormholeInvitationCode`),
            method: 'post',
            data: this.$http.adornData({
              "id": id
            })
          }).then(({data}) => {
            if (data && data.data) {
              this.dataForm = {
                id: data.data.id,
                invitationCode: data.data.invitationCode,
                invitationStatus: data.data.invitationStatus,
                invitationAuthCode: data.data.invitationAuthCode,
                invitationExpireTime: data.data.invitationExpireTime,
                extInfo: data.data.extInfo,
                env: data.data.env
              }
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/invitation/invitation/${!this.dataForm.id ? 'createWormholeInvitationCode' : 'updateWormholeInvitationCode'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'invitationCode': this.dataForm.invitationCode,
              'invitationStatus': this.dataForm.invitationStatus,
              'invitationAuthCode': this.dataForm.invitationAuthCode,
              'invitationExpireTime': this.dataForm.invitationExpireTime,
              'extInfo': this.dataForm.extInfo,
              'env': this.dataForm.env
            })
          }).then(({data}) => {
            if (data && data.success) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.clearData()
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
              this.clearData()
            }
          })
        }
      })
    },
    // 清除表单数据
    clearData() {
      this.dataForm = {
        id: 0,
        invitationCode: '',
        invitationStatus: 'ACTIVE',
        invitationAuthCode: '',
        invitationExpireTime: '',
        extInfo: '',
        env: ''
      }
      this.visible = false
    }
  }
}
</script>
